﻿//------WebPlayer 1.0------//
// Last updated February 28, 2009
// This script interacts with the SoundManager 2 flash movie 
// to play radio station media.
// It handles the pseudo-broadcasting of media, providing facilities for 
// broadcasting as well as for playing single files
//-------------------------//
var interopURL = "/WebPlayer.aspx";
var response = "";
var xmlHttp;

var defaultDisplay = "00:00:00";

var p;
var display;
var playbtn;
var stopbtn;
var managerReady = false;


soundManager.onload = function() {
if (document.getElementById('webPlayer')) {
        Initialize();        
    }
}

var displayStatus = "";
var isPlaying = false;
var currentFile = "";
var nextFile = "";
var nextFileTitle = "";
var elapsed = 0;
var total = 0;

var runSequence = false;

function Initialize() {
    p = soundManager;
    managerReady = true;
    display = document.getElementById("displayMedia");
    playbtn = document.getElementById("playerPlay");
    stopbtn = document.getElementById("playerStop");
    SetPlayHover();
    SetStopHover();
    SetDisplay("");
    if(runSequence)
        PlayFileSequence("Welcome!", "/media/youare.mp3");
}
/*----------
--------*/

//-------Player controls
function PlayFile(playertext, file) {
    SetFile(playertext, file);
    isPlaying = false;
    Play();
}
function PlayFileSequence(playertext, file) {
    SetFile(playertext, file);
    PlaySequence();
}
function SetFile(playertext, file) {
    currentFile = file;
    displayStatus = playertext.replace("/", " . ").replace("/", " . ");
    if(p)
    p.destroySound('currentSong');
}
function Play() {
if(p)
    if (!isPlaying) {
        if (displayStatus != null || currentFile != null) {
            p.createSound({ id: 'currentSong', url: currentFile, whileloading: SetDisplay("Buffering..."), onload: SetDisplay(displayStatus), whileplaying: soundDuration});
        }
        p.play('currentSong');
        isPlaying = true;

        if (!runSequence)
            SetPauseHover();
    }
    else {
        if (runSequence)
            StopFile();
        else {
            p.pause('currentSong');
            isPlaying = false;
            SetPlayHover();
        }
            
    }
}

function PlaySequence() {
    if (!isPlaying) {
        if (displayStatus != null || currentFile != null) {
            p.createSound({ id: 'currentSong', url: currentFile, whileloading: SetDisplay("Buffering..."), onload: SetDisplay(displayStatus), whileplaying: soundDuration, onfinish: Advance });
        }
        p.play('currentSong');
        isPlaying = true;
    }
    else { StopFile(); }
}

function IncreaseVolume() {
    x = p.controls.volume;
    if (x + 10 <= 100) {
        p.controls.volume = (x + 10);
    } 
}
function DecreaseVolume() {
    x = p.controls.volume;
    if (x - 10 >= 0) {
        p.controls.volume = (x - 10);
    }
}
function StopFile() {
    p.stop('currentSong');
    currentFile = "";
    SetDisplay(displayStatus);
    if (document.all) {
        document.getElementById("webPlayerRemaining").innerText = "00:00:00";
    } else {
        document.getElementById("webPlayerRemaining").textContent = "00:00:00";
    }
    SetPlayHover();
    isPlaying = false;
}
function Advance() {
    if (isPlaying) {
        StopFile();
        sendStatus();
    }
}

/*------Set control styles------*/
function SetPlayHover() {
    playbtn.style.background = "url('../../content/images/studio/playbtn.png') 0px 0px";
    playbtn.style.backgroundPosition = "0px 0px";
    playbtn.onmouseover = function() {
    playbtn.style.backgroundPosition = "0px -50px";
    }
    playbtn.onmouseout = function() {
    playbtn.style.backgroundPosition = "0px 0px";
    }
    playbtn.onmouseup = function() {
    playbtn.style.backgroundPosition = "0px -50px";
    }
    playbtn.onmousedown = function() {
    playbtn.style.backgroundPosition = "0px -100px";
    }
    playbtn.value = "";
}
function SetPauseHover() {
    playbtn.style.background = "url('../../content/images/studio/pausebtn.png') 0px 0px";
    playbtn.onmouseover = function() {
        playbtn.style.backgroundPosition = "0px -50px";
    }
    playbtn.onmouseout = function() {
        playbtn.style.backgroundPosition = "0px 0px";
    }
    playbtn.onmouseup = function() {
        playbtn.style.backgroundPosition = "0px -50px";
    }
    playbtn.onmousedown = function() {
        playbtn.style.backgroundPosition = "0px -100px";
    }
    playbtn.value = "";
}
function SetStopHover() {
    stopbtn.style.background = "url('../../content/images/studio/stopbtn.png') 0px 0px";
    stopbtn.onmouseover = function() {
        stopbtn.style.backgroundPosition = "0px -50px";
    }
    stopbtn.onmouseout = function() {
        stopbtn.style.backgroundPosition = "0px 0px";
    }
    stopbtn.onmouseup = function() {
        stopbtn.style.backgroundPosition = "0px -50px";
    }
    stopbtn.onmousedown = function() {
        stopbtn.style.backgroundPosition = "0px -100px";
    }
    stopbtn.value = "";
}
/*--------------End styles---*/

function SetDisplay(text) {
    if (document.all)
        display.innerText = text;
    else
        display.textContent = text;
}

function soundDuration() {

    //timing = (this.position/100000).toFixed(2) + "/" + (this.duration/100000).toFixed(2);
    elapsed =  (this.position / 60000).toFixed(2);
    total = (this.duration / 60000).toFixed(2);

    var elapsedMilli = (this.position / 600).toFixed(2)

    elapsedMin = elapsed.split(".")[0];

    if (elapsedMin < 10)
        elapsedMin = "0" + elapsedMin;
    
    totalMin = total.split(".")[0];

    elapsedSec = ((elapsed.split(".")[1]) * 60 / 100).toFixed(0);

    if (elapsedSec < 10)
        elapsedSec = "0" + elapsedSec;
    
    totalSec = ((total.split(".")[1]) * 60 / 100).toFixed(0);

    var elapsedMillisec = ((elapsedMilli.split(".")[1]) * 60 / 100).toFixed(0);

    if (elapsedMillisec < 10)
        elapsedMillisec = "0" + elapsedMillisec;

    timing = elapsedMin + ":" + elapsedSec + ":" + elapsedMillisec;

    if (document.all) {
        document.getElementById("webPlayerRemaining").innerText = timing;
    } else {
    document.getElementById("webPlayerRemaining").textContent = timing;
}
    
}
//--------End player controls

function sendStatus() {
    sendData("currentfile=" + currentFile + "&elapsed=0&total=0");
}

function getNextMedia() {
    var responseItems = response.split(";");
    var fileName = responseItems[0];
    var fileUrl = responseItems[1];

    if (fileName == "") {
        sendStatus();
        return;
    }
    nextFile = fileUrl;
    nextFileTitle = fileName;
    
    if (!isPlaying)
        PlayFileSequence(nextFileTitle, nextFile);

}
function beginBroadcastTimer() {
   // window.setInterval("startBroadcastSequence()", 5000);
}

function startBroadcastSequence(){
    //sendData("currentfile=" + currentFile + "&elapsed=" + elapsed + "&total=" + total); //Send current media status
    sendStatus(); //See if player should play something next

    if (isPlaying == false)
        getNextMedia();
}

function sendData(data) {

    try
    {  // Firefox, Opera 8.0+, Safari, IE 7+
        xmlHttp = new XMLHttpRequest();
        
    }
    catch (e) 
    {  // Internet Explorer 6
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
        }

        catch (e) 
        {
            try 
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");     
             }
             catch (e) 
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    xmlHttp.open("POST", interopURL, false);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(data);

    try {
        xmlHttp.onreadystatechange = CheckState();
    }
    catch (e) {
    }
}

function CheckState() {
    if (xmlHttp.readyState == 4) {
        response = xmlHttp.responseText;
        if (response.indexOf("<") > -1)
            Initialize();
        if (response.indexOf("|") > -1)
            return;
        if (response == "")
            Initialize();
        getNextMedia();
    }
}